package com.wisdomparking.config.mvc;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;

import com.wisdomparking.tools.ToolValid;

public class BaseController {

    
    @Autowired
    protected HttpServletRequest request;

    @Autowired
    protected HttpServletResponse response;
    
    @Autowired
	protected HttpSession session;

    @SuppressWarnings("unchecked")
    protected <T> T getPara(String name) {
        return (T) request.getParameter(name);
    }

    @SuppressWarnings("unchecked")
    protected <T> T getPara(String name, Object defaultValue) {
        String value = request.getParameter(name);
        return ToolValid.isNotBlank(value) ? (T) value : (T) defaultValue;
    }

    protected String[] getParas(String name) {
        return request.getParameterValues(name);
    }

    public HttpServletRequest getRequest() {
        return request;
    }

    public void setRequest(HttpServletRequest request) {
        this.request = request;
    }

    protected BaseController setAttr(String name, Object value) {
        request.setAttribute(name, value);
        return this;
    }

    protected BaseController removeAttr(String name) {
        request.removeAttribute(name);
        return this;
    }

    @SuppressWarnings("unchecked")
    protected <T> T getAttr(String name) {
        return (T) request.getAttribute(name);
    }

    protected HttpSession getSession() {
    		session = request.getSession();
        return session;
    }
    
    @SuppressWarnings("unchecked")
    protected <T> T getSessionAttr(String key) {
        HttpSession session = request.getSession(false);
        return session != null ? (T) session.getAttribute(key) : null;
    }

    protected BaseController setSessionAttr(String key, Object value) {
        request.getSession(true).setAttribute(key, value);
        return this;
    }

    protected BaseController removeSessionAttr(String key) {
        HttpSession session = request.getSession(false);
        if (ToolValid.isNotNull(session))
            session.removeAttribute(key);
        return this;
    }

    protected String getCookie(String name, String defaultValue) {
        Cookie cookie = getCookieObject(name);
        return cookie != null ? cookie.getValue() : defaultValue;
    }

    protected Cookie getCookieObject(String name) {
        Cookie[] cookies = request.getCookies();
        if (cookies != null)
            for (Cookie cookie : cookies)
                if (cookie.getName().equals(name))
                    return cookie;
        return null;
    }

    protected BaseController removeCookie(String name) {
        return doSetCookie(name, null, 0, null, null, null);
    }

    protected BaseController setCookie(String name, String value) {
        response.addCookie(new Cookie(name, value));
        return this;
    }

    protected String getCookie(String name) {
        return getCookie(name, null);
    }

    private BaseController doSetCookie(String name, String value, int maxAgeInSeconds, String path, String domain,
            Boolean isHttpOnly) {
        Cookie cookie = new Cookie(name, value);
        cookie.setMaxAge(maxAgeInSeconds);
        if (ToolValid.isNull(path)) {
            path = "/";
        }
        cookie.setPath(path);

        if (domain != null) {
            cookie.setDomain(domain);
        }
        if (ToolValid.isNotNull(isHttpOnly)) {
            cookie.setHttpOnly(isHttpOnly);
        }
        response.addCookie(cookie);
        return this;
    }
    
}
